package com.cuijiao.emp.java.service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import com.cuijiao.emp.java.dao.EmpDao;
import com.cuijiao.emp.java.model.Emp;
import com.cuijiao.emp.java.util.DateUtil;

/**
 * @author cuijiao（解耦）不能出现EmpDaoImpl：基于接口开发(稳定)
 */
public class EmpServiceSQL implements EmpService {
    private EmpDao empDao;

    public EmpDao getEmpDao() {
        return empDao;
    }

    /**
     * 1. 通过setter方式注入dao层依赖
     *
     * @param empDao
     */
    public void setEmpDao(EmpDao empDao) {
        this.empDao = empDao;
    }

    /**
     * 2. 通过构造方法 注入dao层依赖
     *
     * @param empDao
     */
    public EmpServiceSQL(EmpDao empDao) {
        this.empDao = empDao;
    }

    @Override
    public void addEmp() {
        // 1.输入员工号:判断员工号是否存在，如存在，重新输入
        String empNo = "";
        System.out.println("请输入员工号");
        boolean isFind = false;
        Emp emp = null;
        Scanner sc = new Scanner(System.in);
        do {
            empNo = sc.next();
            emp = empDao.getEmpByEmpNo(empNo);
            isFind = emp == null ? false : true;
            if (isFind) {
                System.out.println("员工号已存在，请重新输入员工号");
            }
        } while (isFind);
        // 2.添加员工
        sc.nextLine();// 吃掉输入员工号时的回车符
        System.out.println("请输入姓名：");
        String empName = sc.nextLine();
        System.out.println("请输入性别（F/M）");
        String sexStr = sc.next();
        boolean sex = "M".equalsIgnoreCase(sexStr);// m,M,true,男
        System.out.println("请输入薪资");
        double salary = sc.nextDouble();
        System.out.println("请输入生日：（yyyy-MM-dd）");
        String birthdayStr = sc.next();
        Date birthday = null;
        try {
            birthday = DateUtil.string2Date(birthdayStr);
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("日期格式不对");
            return;
        }

        emp = new Emp(empName, empNo, salary, sex, birthday);
        empDao.addEmp(emp);
        System.out.println("添加成功");
    }

    @Override
    public void displayOne() {
        String empNo = "";
        Emp emp = null;// list下标-1表示不存在
        System.out.println("请输入员工号");
        Scanner sc = new Scanner(System.in);
        do {
            empNo = sc.next();
            emp = empDao.getEmpByEmpNo(empNo);
            if (emp == null) {
                System.out.println("员工号不存在，请重新输入员工号");
            }
        } while (emp == null);
        System.out.println(emp);
    }

    @Override
    public void deleteEmp() {
        String empNo = "";
        Emp emp = null;
        System.out.println("请输入员工号");
        boolean isFind = false;
        Scanner sc = new Scanner(System.in);
        do {
            empNo = sc.next();
            emp = empDao.getEmpByEmpNo(empNo);
            isFind = emp == null ? false : true;
            if (!isFind) {
                System.out.println("员工号不存在，请重新输入员工号");
            }
        } while (!isFind);
        empDao.deleteEmpByEmpNo(empNo);
        System.out.println("删除成功！！");
    }

    @Override
    public void modifyEmp() {
        String empNo = "";
        Emp emp = null;
        System.out.println("请输入员工号");
        boolean isFind = false;
        Scanner sc = new Scanner(System.in);
        do {
            empNo = sc.next();
            emp = empDao.getEmpByEmpNo(empNo);
            isFind = emp == null ? false : true;
            if (!isFind) {
                System.out.println("员工号不存在，请重新输入员工号");
            }
        } while (!isFind);
        // 2.修改员工(全部重新修改)
        sc.nextLine();// 吃掉输入员工号时的回车符
        System.out.println("请输入姓名：");
        String empName = sc.nextLine();
        System.out.println("请输入性别（F/M）");
        String sexStr = sc.next();
        boolean sex = "M".equalsIgnoreCase(sexStr);// m,M,true,男
        System.out.println("请输入薪资");
        double salary = sc.nextDouble();
        System.out.println("请输入生日：（yyyy-MM-dd）");
        String birthdayStr = sc.next();
        Date birthday = null;
        try {
            birthday = DateUtil.string2Date(birthdayStr);
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("日期格式不对");
            return;
        }
        emp = new Emp(empName, empNo, salary, sex, birthday);
        empDao.modifyEmpByEmpNo(emp);
        System.out.println("修改成功");
    }

    @Override
    public void dispalayAll() {
        List<Emp> empList = empDao.getEmps();
        if (!empList.isEmpty()) {
            for (Emp emp : empList) {
                System.out.println(emp);
            }
        } else
            System.out.println("暂无员工");

    }
}
